<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <form class="layui-form layuimini-form" lay-filter="edit-form">
        <input name=id value="" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label required">角色名</label>
            <div class="layui-input-block">
                <input type="text" name="roleName" autocomplete="off" lay-verify="required" lay-reqtext="角色名不能为空"
                    placeholder="请输入角色名" value="" class="layui-input">
                <tip>填写自己管理账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序值</label>
            <div class="layui-input-block">
                <input type="number" name="sort" autocomplete="off" placeholder="请输入排序值" value="0" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane="">
            <label class="layui-form-label">权限表</label>
            <div class="layui-input-block" id="authTreeTable">
                <script type="text/html" id="toolbarDemo">
                    <div style="display: inline-block;"><div class="layui-inline layui-btn layui-btn-sm layui-btn-primary" title="全部展开" lay-event="expandAll">全部展开</div><div class="layui-inline layui-btn layui-btn-sm layui-btn-primary" title="全部折叠" lay-event="foldAll">全部折叠</div></div>
                </script>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                <script type="text/html" id="currentTableBar">
                    <div class="layui-btn layui-btn-primary layui-btn-xs data-count-add" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div><div class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></div><div class="layui-btn layui-btn-primary layui-btn-xs data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>
                </script>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remarks" autocomplete="off" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="saveBtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
                <input type="button" class="layui-btn layui-btn-primary goback" value="返回"/>
            </div>
        </div>
    </form>
    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
    <script>
        
        var tableConfig = {};

        function initView(obj) {
            console.log(obj);
            g_obj = obj;
            var findOneUrl = "/dicms/api/desktop/sys_role/findOne";
            var saveOneUrl = "/dicms/api/desktop/sys_role/save";
            layui.use(['form', 'common'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    common = layui.common,
                    $ = layui.$;
                tableConfig = {
                    editMode:0,
                    editFormIdx:0,
                    tableIdx:0,
                    // 表格请求数据地址
                    deleteOneUrl: "/dicms/api/desktop/sys_auth/deleteOne",
                    deleteOneToManyUrl: "/dicms/api/desktop/sys_auth/deleteMany",
                    addPageUrl: '../../page/auth/edit.html',
                    editPageUrl: '../../page/auth/edit.html',
                    currRowObject:{},
                    currRowPObject:{},
                    tableOptions:{
                            elem: '#currentTableId',
                            url: '/dicms/api/desktop/sys_auth/findAllUserAuths',
                            toolbar: '#toolbarDemo',//'default',//
                            height: '100',
                            // skin: 'line',
                            tree: {
                                iconIndex: 2,
                                isPidData: true,
                                idName: 'id',
                                pidName: 'pid'
                            },
                            defaultToolbar: [
                                // 'filter', 'print', 'exports', {
                                // title: '提示',
                                // layEvent: 'LAYTABLE_REFRESH',
                                // icon: 'layui-icon-refresh'}
                            ],
                            cols: [
                                [
                                    { type: 'numbers' },
                                    { type: 'checkbox'},
                                    { field: 'authName', title: '授权名称'},
                                    { field: 'authUrl', title: '权限URL' },
                                    // { title: '类型', templet: '<p>{{d.isMenu?"菜单":"按钮"}}</p>', align: 'center', width: 60 },
                                    { field: 'sort', title: '排序值' },
                                    // { align: 'center', toolbar: '#currentTableBar', title: '操作', width: 160 }
                                ]
                            ],
                            style: `margin-top:0;`,
                            tableStyle:`
                            <style>
                            [lay-filter="currentTableId"] .ew-tree-table-box {

                            }
                            </style>
                            `,
                            done(){
                                if (parent.tableConfig.editMode == 0) {

                                } else if (parent.tableConfig.editMode == 1) {
                                    console.log(obj.data);
                                    let id = obj.data.id;
                                    $.ajax({
                                        url: findOneUrl,
                                        data: { id: id },
                                        type: 'get',
                                        dataType: 'json',
                                        success: function (res) {
                                            console.log(res);
                                            if (res.code !== 0) {
                                                layer.msg(res.msg, function () {
                                                });
                                            } else {
                                                form.val('edit-form', res.data);
                                                if(res.data.authIds&&res.data.authIds!=""){
                                                    let authIds = res.data.authIds.split(",");
                                                    tableConfig.tableIdx.setChecked(authIds);
                                                }
                                            }
                                        },
                                        error: function (res) {
                                            console.log(res);
                                        }
                                    });

                                }
                            }
                        }
                };
                layui.common.initTreeTableView(tableConfig);
                //监听提交
                form.on('submit(saveBtn)', function (data) {
                   
                    data = data.field;
                    if (data.roleName.length > 15) {
                        layer.msg('昵称太长!');
                        return false;
                    }
                    data.authIds= tableConfig.tableIdx.checkStatus().map(function(val){
                        return val.id;
                    });
                    data.authIds = data.authIds.join(",");
                    console.log(data);
                    $("#saveBtn").attr('disabled','disabled');
                    $("#saveBtn").addClass("layui-btn-disabled");
                    $.ajax({
                        url: saveOneUrl,
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                
                                common.refreshAndClose();
                            }
                        },
                        error: function (res) {
                            console.log(res);
                            $("#saveBtn").removeAttr('disabled');
                            $("#saveBtn").removeClass("layui-btn-disabled");
                        }
                    });
                    return false;
                });

                $(".goback").click(function (e) {
                    common.closeIframe();
                });

            });
        }
        
    </script>
</body>

</html>